/**
 * 人员管理下的基础信息模块
 */
const db = require('../db/index')
const { Op } = require("sequelize")
const crypto = require('crypto')

//引入角色模型
const initModels = require('../models/init-models')
const Person = initModels(db).info_person
const moment = require('moment');

//基础信息表格
exports.basicInfo = async (req,res) => {
    try{
        const params =req.body
        await db.sync()
        const where = {};
        Object.keys(params).forEach(key => {
            if (params[key] !== undefined
                && params[key] !== null
                && params[key] !== ''
                && !['name','order','orderName','limit','page'].includes(key)) {
                where[key] = params[key];
            }
            if (params[key] !== undefined && params[key] !== null && params[key] !== '' && key === 'name') {
                where[Op.or] = [
                    { name: {[Op.like]: params[key]} },
                    { enName: {[Op.like]: params[key]} }
                ]
            }
        })
        let { count, rows } = await Person.findAndCountAll({
            where,
            offset: (params.page - 1)*params.limit,
            limit: params.limit*1,
            order: [
                ['star',params.order]
            ]
        })
        res.pageSend('查询成功！',0,rows,count,params.limit,params.page)
    }catch (error){
        res.cc(error)
    }
}
//添加角色
exports.addBasic = async (req,res) => {
    //接收表单数据
    const params = req.body
    try{
        await db.sync()
        //sql语句查询姓名是否被占用
        let result = await Person.findAll({
            where: { name: params.name },
        })
        if(result.length >= 1) return res.cc('姓名被占用，请更换其他姓名！')
        //生成id
        const id = crypto.randomUUID()
        //生成时间
        const createTime = moment().format('YYYY-MM-DD HH:mm:ss')
        const updateTime = moment().format('YYYY-MM-DD HH:mm:ss')
        let addResult = await Person.create({
            ...params,
            id,
            createTime,
            updateTime
        })
        if(addResult === null){
            return res.cc('添加人员失败，请稍后再试！')
        }
        res.cc('添加成功！',0)
    } catch (error) {
        res.cc(error)
    }
}
//修改
exports.updateBasic = async (req,res) => {
    //接收表单数据
    const params = req.body
    try{
        await db.sync()
        //sql语句查询姓名是否被占用
        let result = await Person.findAll({
            where: {
                name: params.name,
                id: {
                    // [Op.ne]表示不等于
                    [Op.ne]: params.id
                }
            },
        })
        if(result.length >= 1) return res.cc('姓名被占用，请更换其他姓名！')
        //更新时间
        const updateTime = moment().format('YYYY-MM-DD HH:mm:ss')
        let addResult = await Person.update({
            ...params,
            updateTime}, {
            where: {
                id: params.id
            }
        })
        if(addResult === null){
            return res.cc('修改人员失败，请稍后再试！')
        }
        res.cc('修改成功！',0)
    } catch (error) {
        res.cc(error)
    }
}
//删除
exports.deleteBasic = async (req,res) => {
    const ids = req.body
    try {
        await db.sync()
        const result = await Person.destroy({
            where:{
                id: {
                    [Op.in]: ids
                }
            }
        })
        if(result === null){
            return res.cc('删除人员失败，请稍后再试！')
        }
        res.cc('删除成功！',0)
    }catch (error){
        res.cc(error)
    }
}
